package com.jintoufs.web.action;

import com.jintoufs.domain.user.User;
import com.jintoufs.service.user.UserService;
import com.jintoufs.web.action.shiro.IncorrectCaptchaException;
import com.jintoufs.web.action.shiro.UnModifyDefaultPasswordException;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.SupportAction;
import org.apache.shiro.authc.*;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

/**
 * 包括登陆、注册、忘记密码等前台Action
 *
 * @author tcq
 */
@Controller
public class IndexAction extends SupportAction {

    @Autowired
    private UserService userService;

    /**
     * 跳转到登陆页面
     *
     * @param
     * @param
     * @param model
     * @return
     * @throws IOException
     * @RequestMapping("login") public String login(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
     * String userPin = getString(request, "userPin");
     * String password = getString(request, "password");
     * if (userPin == null || "".equals(userPin) || password == null || "".equals(password)) {
     * return "/login";
     * }
     * User user = userService.login(userPin.trim(), EncodesUtils.Md5Encoder(password.trim()));
     * // 如果不是空的话，就进行登录
     * if (user != null) {
     * putCurrentUserToSession(request.getSession(), user);
     * return "redirect:dashboard.do";
     * } else {
     * request.setAttribute("error", "用户名或密码错误！");
     * return "/login";
     * }
     * }
     */
    @RequestMapping(value = "/login")
    public String showLoginForm(HttpServletRequest req, Model model) {
        String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
        String username = getString(req, "username");
        String error = null;
        boolean modified = true;
        if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户不存在";
        } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (AuthenticationException.class.getName().equals(exceptionClassName)) {
            error = "无权限访问该目录";
        } else if (IncorrectCaptchaException.class.getName().equals(exceptionClassName)) {
            error = "验证码错误";
        } else if (LockedAccountException.class.getName().equals(exceptionClassName)) {
            error = "账号已被禁用";
        } else if (UnModifyDefaultPasswordException.class.getName().equals(exceptionClassName)) {
            modified = false;
        } else if (ExcessiveAttemptsException.class.getName().equals(exceptionClassName)) {
            error = "登录失败多次，账户锁定10分钟";
        } else if (exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }

        model.addAttribute("username", username);
        model.addAttribute("error", error);
        model.addAttribute("modified", modified);
        if (req.getParameter("forceLogout") != null) {
            model.addAttribute("error", "您已经被管理员强制退出，请重新登录");
        }
        return "login";
    }


    @RequestMapping("dashboard")
    public String dashboard(Byte isLogin, @CurrentUser User user) throws IOException {
        if (isLogin == null || isLogin != 0) {
            user.setLastLoginTime(new Date());
            userService.update(user, new AjaxMsg());
        }

        return "/dashboard";
    }

    /**
     * 退出
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     *//*
    @RequestMapping("loginOut")
	public String loginOut(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
		// deleteKeyCokie(request, response);
		session.removeAttribute("user");
		session.removeAttribute("userImage");
		return "redirect:login.do";
	}*/
}